package model.inventory;

import java.io.File;
import java.text.MessageFormat;

import model.Settings;
import model.contents.ContentType;

import org.json.simple.JSONObject;

import util.PlayerUtilities;

public class InventoryDefinition {
	private static InventoryDefinition _instance;

	private BoxType boxType = BoxType.AUTOBOX;
	private int globalImagePauseTime;
	private int numberOfRecordsToPlayPerCycle;

	private int mainImageContainerId;
	private ContentTypeMapper contentTypeMapper;
	private FieldFormatMapper textFormatMapper;
	private InventoryTableDefinition tableDefinition;

	public enum BoxType {
		AUTOBOX, REALESTATEBOX
	}
	
	public String toString() {
		Object[] values = {globalImagePauseTime, numberOfRecordsToPlayPerCycle, mainImageContainerId};
		String pattern = "Inventory Player: mainImageContainer({2}), imagePauseTime({0}), numberOfRecordsToPlayPerCycle({1})";
		return MessageFormat.format(pattern, values);
		
	}
	
	public FieldFormatMapper getTextFormatMapper() {
		return textFormatMapper;
	}

	public void setTextFormatMapper(FieldFormatMapper textFormatMapper) {
		this.textFormatMapper = textFormatMapper;
	}

	public ContentTypeMapper getContentTypeMapper() {
		return contentTypeMapper;
	}

	public void setTableDefinition(InventoryTableDefinition tableDefinition) {
		this.tableDefinition = tableDefinition;
	}



	public static InventoryDefinition getInstance() {
		// load inventory player from xml file if the data file exists.
		if (_instance == null) {
			File inventoryFile = new File(Settings.INVENTORY_DEF_XML);
			if (inventoryFile.exists()) {
				Object obj = PlayerUtilities.getObjectFromXMLFile(Settings.INVENTORY_DEF_XML);
				if (obj instanceof InventoryDefinition) {
					_instance = (InventoryDefinition) obj;
					return _instance;
				}
			}
			_instance = new InventoryDefinition();
		}
		return _instance;
	}

	public void saveToXML() {
		PlayerUtilities.saveObjectToXMLFile(Settings.INVENTORY_DEF_XML, this);
	}

	private InventoryDefinition() {
		this.reset();
	}

	public String[] getFields() {
		return this.contentTypeMapper.getFieldNames();
	}

	public void setContentTypeMapper(ContentTypeMapper mapper) {
		this.contentTypeMapper = mapper;
	}

	public void addFieldMapping(String fieldName, ContentType type, int containerId) {
		this.contentTypeMapper.put(fieldName, type, containerId);
	}

	public void removeFieldMapping(int containerId) {
		this.contentTypeMapper.removeMapping(containerId);
	}

	public ContentType getContentType(int containerId) {
		return this.contentTypeMapper.get(containerId);
	}

	public BoxType getBoxType() {
		return boxType;
	}

	public void addTextFormatMapping(String fieldName, JSONObject format) {
		this.textFormatMapper.put(fieldName, format);
	}

	public void removeTextFormatMapping(String fieldName) {
		this.textFormatMapper.removeMapping(fieldName);
	}

	public void addTableFieldFormat(String fieldName, JSONObject format) {
		this.tableDefinition.addTableFieldFormat(fieldName, format);
	}

	public void removeTableFieldFormat(String fieldName) {
		this.tableDefinition.removeTableFieldFormat(fieldName);
	}

	public void setBoxType(BoxType boxType) {
		this.boxType = boxType;
	}

	public int getGlobalImagePauseTime() {
		return globalImagePauseTime;
	}

	public void setGlobalImagePauseTime(int globalImagePauseTime) {
		this.globalImagePauseTime = globalImagePauseTime;
	}

	public int getNumberOfRecordsToPlayPerCycle() {
		return numberOfRecordsToPlayPerCycle;
	}

	public void setNumberOfRecordsToPlayPerCycle(int numberOfRecordsToPlayPerCycle) {
		this.numberOfRecordsToPlayPerCycle = numberOfRecordsToPlayPerCycle;
	}

	public int getMainImageContainerId() {
		return mainImageContainerId;
	}

	public void setMainImageContainerId(int mainImageContainerId) {
		this.mainImageContainerId = mainImageContainerId;
	}

	public boolean containsTableInformation() {
		return this.tableDefinition.isValidTableDefinition();
	}

	public InventoryTableDefinition getTableDefinition() {
		return this.tableDefinition;
	}

	public void reset() {
		System.out.println("Resetting inventory");
		boxType = BoxType.AUTOBOX;
		globalImagePauseTime = 5;
		numberOfRecordsToPlayPerCycle = 5;

		mainImageContainerId = -1;
		contentTypeMapper = new ContentTypeMapper();
		textFormatMapper = new FieldFormatMapper();

		this.tableDefinition = new InventoryTableDefinition();
	}

	public JSONObject getTextFormat(String fieldName) {
		return this.textFormatMapper.get(fieldName);
	}
	
	public Integer[] getContainerIds() {
		return this.contentTypeMapper.getContainerIds();
	}

	public String getFieldName(Integer containerID) {
		return this.contentTypeMapper.getFieldName(containerID);
	}
	

}
